package imp

import (
	sqlc "bgs/db/sqlc"
	activityUserPvLogGrpc "bgs/grpc/gen/service/activity_user_pv_log"
	"bgs/grpc/gen/shared/message"
	"context"
)

/****************************************************************************************/
/*                               activity_user_pv_log entity of helper                  */
/****************************************************************************************/

/****************************************************************************************/
/*                               activity_user_pv_log entity of cmd                     */
/****************************************************************************************/

/****************************************************************************************/
/*                               activity_user_pv_log entity of query                            */
/****************************************************************************************/

/****************************************************************************************/
/*                               activity_user_pv_logAggs entity of helper                       */
/****************************************************************************************/

func transformAggsToActivityUserPvCountEntityToPb(row sqlc.ListAggsToActivityUserPvCountByActivityIDsRow) *activityUserPvLogGrpc.AggsToActivityUserPvCountEntity {
	return &activityUserPvLogGrpc.AggsToActivityUserPvCountEntity{
		ActivityId: row.ActivityID,
		ViewCount:  row.ViewCount,
	}
}

func mapAggsToActivityUserPvCountEntityToPb(list []sqlc.ListAggsToActivityUserPvCountByActivityIDsRow, f func(sqlc.ListAggsToActivityUserPvCountByActivityIDsRow) *activityUserPvLogGrpc.AggsToActivityUserPvCountEntity) []*activityUserPvLogGrpc.AggsToActivityUserPvCountEntity {
	r := make([]*activityUserPvLogGrpc.AggsToActivityUserPvCountEntity, len(list))
	for i, item := range list {
		r[i] = f(item)
	}
	return r
}

/****************************************************************************************/
/*                               activity_user_pv_logAggs entity of query                        */
/****************************************************************************************/

// ListAggsToActivityUserPvCountByActivityIDs  列出指定活动访问数量的聚合
func (s *ActivityAPIService) ListAggsToActivityUserPvCountByActivityIDs(ctx context.Context, req *activityUserPvLogGrpc.ListAggsToActivityUserPvCountByActivityIDsRequest) (res *activityUserPvLogGrpc.ListAggsToActivityUserPvCountByActivityIDsResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("activity_user_pv_logImp->ListAggsToActivityUserPvCountByActivityIDs:%v", req)

	res = &activityUserPvLogGrpc.ListAggsToActivityUserPvCountByActivityIDsResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	rows, err := s.dao.Q.ListAggsToActivityUserPvCountByActivityIDs(ctx, req.ActivityIds)
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.AggsToActivityUserPvCounts = mapAggsToActivityUserPvCountEntityToPb(rows, transformAggsToActivityUserPvCountEntityToPb)
	return
}
